package com.huiyujinrong.qiangdanbao.activity.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.SettingActivity;
import com.huiyujinrong.qiangdanbao.activity.my.AboutActivity;
import com.huiyujinrong.qiangdanbao.activity.my.FeedbackActivity;
import com.huiyujinrong.qiangdanbao.activity.my.MessageActivity;
import com.huiyujinrong.qiangdanbao.activity.my.UserInforActivity;
import com.huiyujinrong.qiangdanbao.activity.my.UserNoInforActivity;
import com.huiyujinrong.qiangdanbao.activity.my.UserRechargeActivity;
import com.huiyujinrong.qiangdanbao.bean.UserStatusBean;
import com.huiyujinrong.qiangdanbao.network.Api;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;
import com.huiyujinrong.qiangdanbao.utils.permission.PermissionReq;
import com.huiyujinrong.qiangdanbao.utils.permission.PermissionResult;
import com.huiyujinrong.qiangdanbao.utils.permission.Permissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的
 * 2017/4/11
 * yd
 */
public class FragmentMy extends Fragment implements FragmentMyContract.View {


    @BindView(R.id.toolbar_Title)
    TextView toolbar_title;
    @BindView(R.id.imSubTitle)
    ImageView tool_bar_set;


    private static final int CALL_PHONE = 10;
    private final String USER_STATUS = "user_status";

    private FragmentMyPresenter presenter;

    @BindView(R.id.user_phone)
    TextView user_phone;

    @BindView(R.id.is_infor)
    TextView is_infor;

    @BindView(R.id.tvPhone)
    TextView tvPhone;
    private Toast t;

    private UserStatusBean u_bean;
    private boolean isFirstTrade;

    private Intent intent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_my, container, false);
        ButterKnife.bind(this, v);
        toolbar_title.setVisibility(View.GONE);
        tool_bar_set.setVisibility(View.VISIBLE);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new FragmentMyPresenter(this, getActivity());
        presenter.start();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        presenter.selectUserStatus(String.valueOf(SharedHelper.get(getActivity(), Api.USER_ID, Api.nul)));
    }


    @Override
    public void onResume() {
        super.onResume();
        presenter.selectUserStatus(String.valueOf(SharedHelper.get(getActivity(), Api.USER_ID, Api.nul)));
        switch (Integer.parseInt(SharedHelper.get(getActivity(), "user_status", 0).toString())) {
            case 1:
                is_infor.setText("未认证");
                break;
            case 2:
                is_infor.setText("审核中");
                break;
            case 3:
                is_infor.setText("已认证");
                break;
            case 4:
                is_infor.setText("认证失败");
                break;
        }
    }

    @OnClick({R.id.my_isformation, R.id.my_money,
            R.id.my_message, R.id.my_about,
            R.id.llService, R.id.llFeedBack,
            R.id.llTop})
    void onClicks(View view) {
        switch (view.getId()) {
            //个人信息
            case R.id.my_isformation:
                toAuthInfo();
                break;
            case R.id.my_money:
                intent = new Intent(getActivity(), UserRechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.my_message:
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.my_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.llService://联系客服
                toContactService();
                break;
            case R.id.llFeedBack://意见反馈
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
            case R.id.llTop:
                toAuthInfo();
                break;
        }


    }

    private void toContactService() {
        if (Build.VERSION.SDK_INT < 23) {
            showDialog();
        } else {

            PermissionReq.with(getActivity())
                    .permissions(Permissions.PHONE)
                    .result(new PermissionResult() {
                        @Override
                        public void onGranted() {
                            showDialog();
                        }

                        @Override
                        public void onDenied() {
                            Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
                            startActivity(intent);
                        }
                    }).request();
        }
    }

    private void toAuthInfo() {
        switch (u_bean.getUser_status()) {
            case 1:
                Intent intent1 = new Intent(getActivity(), UserNoInforActivity.class);
                intent1.putExtra("status", "未认证");
                startActivity(intent1);
                break;
            case 2:
                Intent intent3 = new Intent(getActivity(), UserInforActivity.class);
                intent3.putExtra("status_num", u_bean.getUser_status());
                startActivity(intent3);
                break;
            case 3:
                Intent intent = new Intent(getActivity(), UserInforActivity.class);
                intent.putExtra("status_num", u_bean.getUser_status());
                startActivity(intent);
                break;
            case 4:
                Intent intent2 = new Intent(getActivity(), UserNoInforActivity.class);
                intent2.putExtra("status", "认证失败");
                startActivity(intent2);
                break;
        }
    }


    /**
     * 跳转至设置 界面
     */
    @OnClick(R.id.imSubTitle)
    void gotoSetA() {
        startActivity(new Intent(getActivity(), SettingActivity.class));
    }

    /**
     * 显示对话框
     */
    private void showDialog() {
        String phone = tvPhone.getText().toString().trim();
        new AlertDialog.Builder(getActivity())
                .setTitle("温馨提示").setMessage("拨打电话：" + phone)
                .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        toCallPhone();
                    }
                })
                .create()
                .show();
    }

    /**
     * 打电话
     */
    private void toCallPhone() {
        String phone = tvPhone.getText().toString().trim();
        if (!TextUtils.isEmpty(phone)) {
            String ph = phone.replaceAll("-", "");
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ph));
            startActivity(intent);
        } else {
            showToast("电话号码不能为空");
        }
    }

    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void setViewData(UserStatusBean bean) {
        SharedHelper.put(getActivity(), "user_status", bean.getUser_status());
        u_bean = bean;
        user_phone.setText(bean.getPhone());

        switch (u_bean.getUser_status()) {
            case 1:
                is_infor.setText("未认证");
                break;
            case 2:
                is_infor.setText("审核中");
                break;
            case 3:
                is_infor.setText("已认证");
                break;
            case 4:
                is_infor.setText("认证失败");
                break;
        }
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.end();
    }
}
